angular.module('${menuCode}')
.controller("${widgetCode}Ctrl",function($scope,AppKit){
	var tmpList = [];
	$scope.loadModuleData=function(){
		var url = '/portal/index?MobileDataProvider&actionType=findAppModuleList';
		AppKit.getJsonApi(url).success(function(jsonData){
			tmpList = jsonData.moduleList;
			$scope.list = tmpList;
		});
		}
	$scope.loadModuleData();
	  
	$scope.sortableOptions = {
//	    update: function(e, ui) {
//	      var logEntry = tmpList.map(function(i){
//	        return i.id;
//	      }).join(', ');
//	      $scope.sortingLog.push('Update: ' + logEntry);
//	    },
	    stop: function(e, ui) {
	    	 $scope.sortingLog = [];
	      var logEntry = tmpList.map(function(i){
	        return i.id;
	      });
	      $scope.sortingLog = logEntry;
	      var url = "/portal/index?MobileDataProvider&actionType=mobileSaveAppModule&mark=sort";
	  		AppKit.postJsonApi(url,$scope.sortingLog).then(function(response){
	  		});
	    }
	};	 
	
	$scope.addAppModal = function(){
		$scope.modalTitle = "待选应用模块";
		AppKit.createModal("tochoose-module-modal",$scope);			
	}
	
	$scope.toChooseModuleData=function(){
		var url = '/portal/index?MobileDataProvider&actionType=findToChooseAppModuleList';
		var promise = AppKit.getJsonApi(url);
		promise.success(function(rspJson){
			$scope.toChooseModuleList = rspJson.toChooseModuleList;
		});
	}
	
	$scope.deleteAppModal = function(id){
	      var url = "/portal/index?MobileDataProvider&actionType=mobileSaveAppModule&mark=delete&id="+id;
		  		AppKit.postJsonApi(url,$scope.sortingLog).then(function(response){
		  			if ("success" == response.data){
						$scope.loadModuleData();
					}else{
						AppKit.errorPopup();
					}
		  		});

	}
	
	$scope.doSelectModule = function(){
		$scope.receiveList = [];
		for(var i=0;i < $scope.toChooseModuleList.length;i++){
			var item =  $scope.toChooseModuleList[i];
			if(item.checked){
				$scope.receiveList.push(item);
			}
		}
		var url = "/portal/index?MobileDataProvider&actionType=mobileSaveAppModule&mark=selected";
		AppKit.postJsonApi(url,$scope.receiveList).then(function(response){
			if ("success" == response.data){
				$scope.loadModuleData();
			}else{
				AppKit.errorPopup();
			}
		});
	}
});